package com.slx.sthq.config;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

@Component
public class SwaggerPrintConfig implements ApplicationListener<WebServerInitializedEvent> {
    private static final Logger log = LoggerFactory.getLogger(SwaggerPrintConfig.class);

    @Override
    public void onApplicationEvent(WebServerInitializedEvent webServerInitializedEvent) {
        try {
            // 获取IP

            String hostAddress = InetAddress.getLocalHost().getHostAddress();
            // 获取端口号
            int port = webServerInitializedEvent.getWebServer().getPort();
            // 获取应用名
            String applicationName = webServerInitializedEvent.getApplicationContext().getId();
            String[] activeProfiles = webServerInitializedEvent.getApplicationContext().getEnvironment().getActiveProfiles();
            log.info("{} 启动成功！", applicationName);
            log.info("当前加载配置文件为 {}", Arrays.toString(activeProfiles));
            log.info("swagger接口文档地址：http://{}:{}/doc.html", hostAddress, port);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

    }

}
